// http://localhost:3000
// http://localhost:3000/login/cellphone?phone=18170948231&password=zlgz5958

const request = {
	// vercelUrl: 'https://cloud-music-api-gamma.vercel.app/',
	vercelUrl: 'http://192.168.0.105:3000',
	ipUrl: 'http://192.168.0.105:3000',
	localUrl: 'http://localhost:3000',
	// 微信获取用户信息进行登陆(未完成)
	loginByWX(){
		uni.getProvider({
		    service: 'oauth',
		    success: function (res) {
		        if (~res.provider.indexOf('weixin')) {
		            uni.login({
		                provider: 'weixin',
		                success: function (loginRes) {
											// console.log(JSON.stringify(loginRes));
											uni.getUserInfo({
												provider: 'weixin',
												success: function (infoRes) {
													console.log(JSON.stringify(infoRes));
												}
											})
		                }
		            });
		        }
		    }
		})
	},
	
	// 查询登陆状态
	queryloginStatus() {
		let that = this
		uni.request({
			url: `${that.vercelUrl}/login/status`,
			success:function(res){
				console.log('登陆状态为:', res);
			}
		})
	},
	
	// 手机登陆接口
	loginByPhoneNumber(phonenumber, password) {
		let that = this
		// 检查输入的手机号是否注册
		uni.request({
			url: `${that.vercelUrl}/cellphone/existence/check?phone=${phonenumber}`,
			success:function(res){
				if (res.data.exist < 0) {
					uni.request({
						title: '您输入的账号未注册',
						icon: 'none'
					})
				} else {
					// 检测账号为已注册账号，再发送获取用户信息请求
					uni.request({
						url: `${that.vercelUrl}/login/cellphone?phone=${phonenumber}&password=${password}`,
						success: (loginRes) => {
							if (loginRes.data.code === 200) {
								console.log(loginRes);
								let userInfo = {
									cookie: loginRes.data.cookie,
									uid: loginRes.data.account.id,
									uname: loginRes.data.profile.nickname,
									avatarUrl: loginRes.data.profile.avatarUrl
								}
								// 登陆成功则将用户数据保存至缓存中
								uni.setStorage({
								    key: 'userInfo',
								    data: userInfo,
								    success: function () {
								        console.log('set cookies Storage success');
												uni.showToast({
													title: '登录成功',
													icon: 'none',
													success:function(){
														// 登陆成功后跳转至发现页面
														uni.switchTab({
															url: '../discovery/discovery'
														})
													}
												})
												
								    }
								})
							} else if (loginRes.data.code === 502){
								uni.showToast({
									title: '密码错误',
									icon: 'none'
								})
							} else {
								uni.showToast({
									title: '登录失败',
									icon: 'none'
								})
							}
						},
						fail:function(err){
							console.log(JSON.stringify(err));
							uni.showToast({
								title: '请求登录超时'
							})
						}
					})
				}
			}
		})
		
	},
	
	// 退出登录
	logout() {
		let that = this
		uni.request({
			url: `${that.vercelUrl}/logout`,
			success:function(res){
				console.log('退出结果为：',res);
				if (res.data.code === 200) {
					// 状态码为200则清除缓存，并提示退出成功
					uni.clearStorageSync()
					uni.showToast({
						title: '退出成功',
						icon: 'none'
					})
				}
			}
		})
	},
	
	// 获取用户详情
	getUserInfo(uid) {
		let that = this
		uni.request({
			url: `${that.vercelUrl}/user/detail?uid=${uid}`,
			success:function(res){
				console.log(res);
			}
		})
	},
	
	// 获取账号信息(需要携带登陆cookie)
	getAccountInfo() {
		let that = this
		uni.getStorage({
			key: 'userInfo',
			success:function(res){
				// console.log('本地缓存：', res);
				uni.request({
					url: `${that.vercelUrl}/user/account`,
					header: {
						cookie: res.data.cookie
					},
					success: accountData => {
						console.log('账户信息：', accountData);
					},
					fail:function(e) {
						console.log(e)
					}
				})
			},
			fail:function(e){
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				})
			}
		})
	},
	
	// 获取用户信息, 歌单收藏，mv, dj数量(需要携带登陆cookie)
	getUserSongSheet() {
		let that = this
		uni.getStorage({
			key: 'userInfo',
			success:function(res){
				// console.log(res);
				uni.request({
					url: `${that.vercelUrl}/subcount`,
					header: {
						cookie: res.data.cookie
					},
					success: res => {
						console.log(res);
					},
					fail:function(e) {
						console.log(e)
					}
				})
			},
			fail:function(e){
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				})
			}
		})
	},
	
	// 获取默认搜索关键词请求————定义在discovery组件
	
	
	// 获取轮播图(banner)的相关数据(活动链接，独家歌曲ID)
	getBanner(bannerArray){
		let that = this
		uni.request({
			url: `${that.vercelUrl}/banner?type=2`,
			success: res => {
				// console.log("轮播图数据:", res.data.banners);
				res.data.banners.forEach(item => {
					bannerArray.push(item)
				})
			}
		})
	}
}

export default request